package com.jy.appmarket.newnetwork.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jy.app.market.idata.Doc;

public class ApiResult {
	private final int sequence;
	private JSONObject json;

	private Doc doc;

	public ApiResult(int sequence, String content) {
		this.sequence = sequence;
		try {
			json = new JSONObject(content);
		} catch (Exception e) {
			json = null;
		}

		this.doc = Doc.fromJson(content);
	}

	public int getSequence() {
		return this.sequence;
	}

	//////////////////////////////////////////
	// Doc方式解析结果
	public boolean isOK() {
		return doc.isOk();
	}

	public <T> T getData(Class<?> clz) {
		if (clz == null) {
			return null;
		}
		T data = doc.getData();
		if (clz.isInstance(data)) {
			return data;
		}
		return null;
	}

	////////////////////////////////////////////
	// 自行解析
	public boolean isOKOwn() {
		if (json == null) {
			return false;
		}
		try {
			return (200 == json.getInt("status"));
		} catch (JSONException e) {
		}
		return false;
	}

	public JSONObject getDataJsonObject() {
		try {
			return json.getJSONObject("data");
		} catch (JSONException e) {
		}
		return null;
	}

	public JSONArray getDataJsonArray() {
		try {
			return json.getJSONArray("data");
		} catch (JSONException e) {
		}
		return null;
	}
}